//
//  TotalTractionWeigthErroeController.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2025/9/24.
//  Copyright © 2025 GCY. All rights reserved.
//

#import "TotalTractionWeigthErroeController.h"
#import "TZImagePickerController.h"
#import "SelectedImagUpDataImagView.h"
#import "AutherTicationCellTableViewCell.h"
#import "IDCardTitleTextTableViewCell.h"
#import "IsTheTrailerIsOverloadedView.h"
#import "HandCarAuthenticationController.h"
#import "BreakBoundsAuthenticationController.h"
#import "HandCarAuthenticationController.h"
@interface TotalTractionWeigthErroeController ()<UITableViewDelegate, UITableViewDataSource,TZImagePickerControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (strong , nonatomic)UITableView *tableView;
@property (nonatomic, strong) UIImagePickerController *imagePickerController;
@property (strong, nonatomic) SelectedImagUpDataImagView *selectedTypeView;
@property (strong, nonatomic)IsTheTrailerIsOverloadedView *isChaoview;//是否是超限牌
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) NSIndexPath *curentIndexPath;
@property (strong,nonatomic) UIButton *haveGuaCarNexButton;
@property (strong,nonatomic) UIButton *noHaveGauCarNexButton;

@end

@implementation TotalTractionWeigthErroeController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"准牵引质量识别异常";
    
    self.view.backgroundColor = BackgroundColor;
    [self newDataArr];
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.haveGuaCarNexButton];
    [self.view addSubview:self.noHaveGauCarNexButton];
}
- (void)newDataArr{
    NSString *urlStr;
    for (NSDictionary *dict in self.topDataArr) {
        if ([dict[@"title"] isEqualToString:@"车辆行驶证副页上传"]) {
            urlStr = dict[@"imgURL"];
        }
    }
    self.dataArr = [@[] mutableCopy];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@"车辆行驶证副页上传" forKey:@"title"];
    [dic setValue:urlStr forKey:@"imgURL"];
    [dic setValue:@"0" forKey:@"upType"];//0没识别之前 1 成功。2 失败
    [dic setValue:@"识别失败(点击重新上传)" forKey:@"falseTitle"];
    [dic setValue:@"曝光严重无法准确识别文字内容" forKey:@"falseCentent"];
    [dic setValue:@"cheliangxingshizhengfuye" forKey:@"defaultImag"];
    [dic setValue:@"1" forKey:@"isImagType"];
    [dic setValue:@"cheliangxingshizhengfuyeshilie" forKey:@"ballbackImag"];
    [self.dataArr addObject:dic];
    NSLog(@"%@----",self.dataArr);
}
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArr[indexPath.row];
    if ([dic[@"isImagType"] isEqualToString:@"1"]) {
        return 258;
    }else{
        return 50;
    }
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArr[indexPath.row];
    if ([dic[@"isImagType"] isEqualToString:@"1"]) {
        AutherTicationCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AutherTicationCellTableViewCell"];
//        [cell.upImagButton addTarget:self action:@selector(updataImage:) forControlEvents:(UIControlEventTouchUpInside)];
//        [cell.topBakButton addTarget:self action:@selector(updataImage:) forControlEvents:(UIControlEventTouchUpInside)];
        [cell assignmentDic:self.dataArr[indexPath.row]];
        return cell;
    }else{
        IDCardTitleTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IDCardTitleTextTableViewCell"];
        cell.nameTitleLabel.text = dic[@"title"];
        cell.cententTextLabel.text = dic[@"textContent"];
        return cell;
    }

}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 100)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, ScreenW - 30, 80)];
    label.text = @"提示：检测到您录入的车辆准牵引总质量不为空，可能需要继续完善挂车信息，不完善可能影响您后续在平台上的正常接单业务。如果此处不完善挂车信息的，您也可以选择到”我的>车辆管理“中继续完善信息。";
    label.textColor = [UIColor colorWithRed:237/255.0 green:107/255.0 blue:134/255.0 alpha:1];
    label.font = [UIFont systemFontOfSize:14];
    label.numberOfLines = 0;
    [view addSubview:label];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NavHeight - MenuBarHeight  - 50);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = BackgroundColor;
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:@"AutherTicationCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"AutherTicationCellTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"IDCardTitleTextTableViewCell" bundle:nil] forCellReuseIdentifier:@"IDCardTitleTextTableViewCell"];
    }
    
    return _tableView;
}

- (UIButton *)haveGuaCarNexButton{
    if (_haveGuaCarNexButton == nil) {
        self.haveGuaCarNexButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.haveGuaCarNexButton.frame = CGRectMake(15, ScreenH - MenuBarHeight - 50 - NavHeight, (ScreenW - 50)/2, 50);
        self.haveGuaCarNexButton.tag = 101;
        [self.haveGuaCarNexButton addTarget:self action:@selector(commitUpDataButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        self.haveGuaCarNexButton.backgroundColor = Theme_ColorValue;
        [self.haveGuaCarNexButton setTitle:@"有挂车,去完善" forState:(UIControlStateNormal)];
        [self.haveGuaCarNexButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        self.haveGuaCarNexButton.layer.masksToBounds = YES;
        self.haveGuaCarNexButton.layer.cornerRadius = 10;
    }
    return _haveGuaCarNexButton;
}
- (UIButton *)noHaveGauCarNexButton{
    if (_noHaveGauCarNexButton == nil) {
        self.noHaveGauCarNexButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.noHaveGauCarNexButton.frame = CGRectMake(CGRectGetMaxX(self.haveGuaCarNexButton.frame) + 10, ScreenH - MenuBarHeight - 50 - NavHeight, (ScreenW - 50)/2, 50);
        self.noHaveGauCarNexButton.tag = 102;
        [self.noHaveGauCarNexButton addTarget:self action:@selector(commitUpDataButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        self.noHaveGauCarNexButton.backgroundColor = Theme_ColorValue;
        [self.noHaveGauCarNexButton setTitle:@"无挂车,去审核" forState:(UIControlStateNormal)];
        [self.noHaveGauCarNexButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        self.noHaveGauCarNexButton.layer.masksToBounds = YES;
        self.noHaveGauCarNexButton.layer.cornerRadius = 10;
    }
    return _noHaveGauCarNexButton;
}
- (void)commitUpDataButtonAction:(UIButton *)btn{
    
    if (btn.tag == 102) {//确认上传审核
        [self newworking];
    }else if (btn.tag == 101){//有挂车
        self.isChaoview = [[IsTheTrailerIsOverloadedView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        [self.isChaoview.isChaoButton addTarget:self action:@selector(isChaoButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        [self.isChaoview.isGuaNextButton addTarget:self action:@selector(isGuaNextButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        [[UIApplication sharedApplication].keyWindow addSubview:self.isChaoview];
    }
   
    
}
- (void)newworking{
    NSString * vehicleLicenseOCRFrontStr = [self dicZhuanJsonStr:self.xszZdic];
    NSString * vehicleLicenseOCRBackStr = [self dicZhuanJsonStr:self.xszFdic];
    NSString * newVehicleLicenseOCRBackStr = [self dicZhuanJsonStr:self.xszZXFdic];
    NSString * licensePlateOCRStr  = [self dicZhuanJsonStr:self.ctdic];
    NSString * roadTransportPermitOCRStr = [self dicZhuanJsonStr:self.yszdic];
    NSString *vehicleLicenseImg;
    NSString *vehicleLicenseDeputyPageImg;
    NSString *newVehicleLicenseDeputyPageImg;
    NSString *vehicleImg;
    NSString *roadTransportCertificateImg;
    NSString *roadPermitNumberImg;
    for (NSDictionary *dict in self.topDataArr) {
        if ([dict[@"title"] isEqualToString:@"车辆行驶证正页上传"]) {
            vehicleLicenseImg = dict[@"imgURL"];
        }else if ([dict[@"title"] isEqualToString:@"车辆行驶证副页上传"]){
            vehicleLicenseDeputyPageImg = dict[@"imgURL"];
        }else if ([dict[@"title"] isEqualToString:@"车辆行驶证最新到期日期页照片"]){
            newVehicleLicenseDeputyPageImg = dict[@"imgURL"];
        }else if ([dict[@"title"] isEqualToString:@"带有车牌的车头上传"]){
            vehicleImg = dict[@"imgURL"];
        }else if ([dict[@"title"] isEqualToString:@"道路运输证上传"]){
            roadTransportCertificateImg = dict[@"imgURL"];
        }else if ([dict[@"title"] isEqualToString:@"道路运输证年审凭证(非必传)"]){
            roadPermitNumberImg = dict[@"imgURL"];
        }
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:vehicleLicenseOCRFrontStr forKey:@"vehicleLicenseOCRFrontStr"];
    [dic setValue:vehicleLicenseOCRBackStr forKey:@"vehicleLicenseOCRBackStr"];
    [dic setValue:newVehicleLicenseOCRBackStr forKey:@"newVehicleLicenseOCRBackStr"];
    [dic setValue:licensePlateOCRStr forKey:@"licensePlateOCRStr"];
    [dic setValue:roadTransportPermitOCRStr forKey:@"roadTransportPermitOCRStr"];
    [dic setValue:vehicleLicenseImg forKey:@"vehicleLicenseImg"];
    [dic setValue:vehicleLicenseDeputyPageImg forKey:@"vehicleLicenseDeputyPageImg"];
    [dic setValue:newVehicleLicenseDeputyPageImg forKey:@"newVehicleLicenseDeputyPageImg"];
    [dic setValue:vehicleImg forKey:@"vehicleImg"];
    [dic setValue:roadTransportCertificateImg forKey:@"roadTransportCertificateImg"];
    [dic setValue:roadPermitNumberImg forKey:@"roadPermitNumberImg"];
    [dic setValue:[UserDefaultUtil valueForKey:USER_CODE]  forKey:@"teamCode"];
    [HTTPManager jsonNetworkRequestWithMethod:@"POST" urlString:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], VEHICLE_VEHICLEOCRINFO) withParameter:dic withVC:self netName:@"无挂车图片上传车辆" success:^(id response, NSString *msg) {
        [XSInfoView showInfo:msg onView:self.view.window];
        [self.navigationController popToRootViewControllerAnimated:YES];
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
}
- (NSString *)dicZhuanJsonStr:(NSDictionary *)dic{
    NSError *error;
    if (dic) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&error];
        if (!jsonData) {
            NSLog(@"Error converting DiC%@",error);
            return @"";
        }else{
            return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }
    }else{
        return @"";
    }
    
}


- (void)isChaoButtonAction{
    BreakBoundsAuthenticationController *VC = [[BreakBoundsAuthenticationController alloc] init];
    VC.isChaoStr = @"1";//是否是超牌车 0不是。1是
    VC.topDataArr = self.dataArr;
    VC.xszZdic = self.xszZdic;
    VC.xszFdic = self.xszFdic;
    VC.xszZXFdic = self.xszZXFdic;
    VC.ctdic = self.ctdic;
    VC.yszdic = self.yszdic;
    [self.isChaoview removeFromSuperview];
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)isGuaNextButtonAction{
        HandCarAuthenticationController *VC = [[HandCarAuthenticationController alloc] init];
    VC.isChaoStr = @"0";//是否是超牌车 0不是。1是
    VC.topDataArr = self.dataArr;
    VC.xszZdic = self.xszZdic;
    VC.xszFdic = self.xszFdic;
    VC.xszZXFdic = self.xszZXFdic;
    VC.ctdic = self.ctdic;
    VC.yszdic = self.yszdic;
    [self.isChaoview removeFromSuperview];
    [self.navigationController pushViewController:VC animated:YES];
}
@end
